package com.sense.kukoo.biz_explore.view

import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.sense.kukoo.biz_explore.R
import com.sense.kukoo.biz_explore.adapter.MusicCollectionAdapter
import com.sense.kukoo.biz_explore.databinding.BizExploreActivityMusicCollectionBinding
import com.sense.kukoo.biz_explore.vm.MusicCollectionViewModel
import com.sense.kukoo.common.constant.ARouterUrl
import com.sense.kukoo.common.mvvm.view.BaseMvvmActivity
import com.sense.kukoo.common.repo.bean.BookBean
import com.sense.kukoo.common.repo.bean.ExploreContentBean
import com.sense.kukoo.common.repo.bean.MusicCollectionBean

@Route(path = ARouterUrl.ROUTE_EXPLORE_MODULE_MUSIC_COLLECTION_ACTIVITY)
class MusicCollectionActivity :
    BaseMvvmActivity<MusicCollectionViewModel, BizExploreActivityMusicCollectionBinding>() {

    private val TAG: String = javaClass.simpleName
    private var mAdapter:MusicCollectionAdapter? = null

    @JvmField
    @Autowired(name = ARouterUrl.KEY_EXPLORE_CONTENT)
    var from: ExploreContentBean? = null

    override fun getLayoutId(): Int = R.layout.biz_explore_activity_music_collection

    override fun getViewModelClass(): Class<MusicCollectionViewModel> =
        MusicCollectionViewModel::class.java

    override fun responseVM(action: Int) {}

    override fun initData() {
        ARouter.getInstance().inject(this)

        mBinding.lifecycleOwner = this
        mBinding.vm = mViewModel
        mBinding.titleView.init(this)
        setSupportActionBar(mBinding.titleView)
        //
        mViewModel.initData(from!!)
        //
        mViewModel.mMusicCollection.observe(this){
            mBinding.refreshLayout.finishRefresh()
            mBinding.refreshLayout.finishLoadMore()
            mAdapter?.list = it
            mAdapter?.notifyDataSetChanged()
        }
        mViewModel.enableLoadMore.observe(this){
            mBinding.refreshLayout.setEnableLoadMore(it)
        }
    }

    override fun initView() {
        mBinding.titleView.setTitle(from!!.title)
        //
        val mutableListOf = mutableListOf<MusicCollectionBean>()

        mAdapter = MusicCollectionAdapter(mListener, mutableListOf)
        mBinding.recyclerView.adapter = mAdapter
        //
        val linearLayoutManager = LinearLayoutManager(this)
        mBinding.recyclerView.layoutManager = linearLayoutManager
        mBinding.refreshLayout.setOnRefreshListener {
            //OnRefresh
            mViewModel.refreshData()
        }
        mBinding.refreshLayout.setOnLoadMoreListener {
            //OnLoadMore
            mViewModel.loadMoreData()
        }

        //首次加载
        mViewModel.refreshData()
    }

    val mListener = object : MusicCollectionAdapter.MusicCollectionEventListener {
        override fun onItemClick(book: BookBean, position: Int) {
            ARouter.getInstance()
                .build(ARouterUrl.ROUTE_MEDIA_MODULE_MEDIA_LIST_DETAIL_ACTIVITY)
                .withString(ARouterUrl.KEY_BOOK_ID_STR, ""+book.id)
                .navigation()
        }
    }

}